#include <list>
FILE *CoverFp;
class Cover
{
private:
	
	class PlusData
	{
	public:
		int left, right, top, bottom;
		int CenX, CenY;
		int Area;
	};
	PlusData TmpData;
	bool DrawSwitch;
public:
	Cover()
	{
		DrawSwitch = false;
		//FirstReadCover();
	}
	~Cover()
	{
	}

	list<PlusData> Contain;
	list<PlusData>::iterator Pointer;
	char FolderName[50],AdressName[50];
	void DrawCover();
	void DeleteLast();
	void DeleteAll();
	void SaveCover();
	void NewData(int, int, int, int, int, int, int);
	void NewData();
	void FirstReadCover();
	bool TurnSwitch();
};



void Cover::FirstReadCover()
{
	if((CoverFp=fopen(AdressName,"r"))!=NULL)
	{
		printf("file open\n");
	}
	else
	{
		MessageBox(NULL, AdressName,"(FirstReadCover)file didn't been created",MB_ICONASTERISK);
		_mkdir(FolderName);
		CoverFp=fopen(AdressName,"w+");
	}
	//int kk = 0;;

	rewind(CoverFp);

	Pointer = Contain.begin();
	while(!feof(CoverFp))
	{
		Contain.push_back(TmpData);
		Pointer++;
		
		fscanf(CoverFp, "%d", &(Pointer->CenX)  );
		fscanf(CoverFp, "%d", &(Pointer->CenY)  );
		fscanf(CoverFp, "%d", &(Pointer->Area)  );
		fscanf(CoverFp, "%d", &(Pointer->left)  );
		fscanf(CoverFp, "%d", &(Pointer->top)   );
		fscanf(CoverFp, "%d", &(Pointer->right) );
		fscanf(CoverFp, "%d", &(Pointer->bottom));
		
		//cout<<Pointer->bottom<<endl;	
	};

	Pointer--;
	Contain.pop_back();


	fclose(CoverFp);

}



void Cover::DeleteLast()
{
	Pointer--;
	Contain.pop_back();
	//remove( "myfile.txt" );
}

void Cover::DeleteAll()
{
	Contain.clear();
	Pointer = Contain.begin();
	//remove all
}


void Cover::SaveCover()
{
	if((CoverFp=fopen(AdressName,"w"))!=NULL)
	{
		printf("file open\n");
	}
	else
	{
		MessageBox(NULL, AdressName,"(SaveCover)Cannot fine AdressName",MB_ICONHAND);
		exit(0);
	}
	Pointer = Contain.begin();
	char writedata[22];
	while(Pointer!= Contain.end())
	{
		/*---------------------write data-------------------*/
		sprintf(writedata, "%d %d\n", Pointer->CenX, Pointer->CenY);
		fwrite(writedata, sizeof(char), strlen(writedata), CoverFp);
		sprintf(writedata, "%d\n", Pointer->Area);
		fwrite(writedata, sizeof(char), strlen(writedata), CoverFp);
		sprintf(writedata, "%d %d %d %d\n", 
			Pointer->left, Pointer->top, Pointer->right, Pointer->bottom);
		fwrite(writedata, sizeof(char), strlen(writedata), CoverFp);
		/*--------------------------------------------------*/

		Pointer++;
	}
	//Pointer--;

	fclose(CoverFp);
}


void Cover::NewData(int CenX, int CenY, int Area, int left, int top, int right, int bottom)
{
	

	Contain.push_back(TmpData);
	
	Pointer++;
	
	Pointer->CenX = CenX;
	Pointer->CenY = CenY;
	Pointer->Area = Area;
	Pointer->left = left;
	Pointer->top  = top ;
	Pointer->right= right;
	Pointer->bottom = bottom;

}
void Cover::NewData()
{
	
	Contain.push_back(TmpData);
	
	Pointer++;
	
	Pointer->CenX = (DataPt1.downX + DataPt1.upX)/2;
	Pointer->CenY = (DataPt1.downY + DataPt1.upY)/2;
	Pointer->Area = DataPt1.Area;
	Pointer->left = DataPt1.downX;
	Pointer->top  = DataPt1.downY ;
	Pointer->right= DataPt1.upX;
	Pointer->bottom = DataPt1.upY;
}

void Cover::DrawCover()
{
	//DrawSwitch = !DrawSwitch;
	if(DrawSwitch)
	{
		Pointer = Contain.begin();

		while(Pointer != Contain.end())
		{
			//Draw
			Rectangle(simple_DC, Pointer->left, Pointer->top, Pointer->right, Pointer->bottom);
			//cout<<Pointer->Area<<endl;
			Pointer++;
		}
	}
	Pointer--;
	//else
	//	show_bmppicture(simple_DC,(BYTE *)DataPt1.PicNameAddress, 0 ,0 ,0 ,0 ,1.0,1.0 );
}
bool Cover::TurnSwitch()
{
	DrawSwitch = !DrawSwitch;
	return DrawSwitch;
}